<template>
  <div class="login-container">
    <div class="content">
      <div class="title">xxx管理系统 -- 演示版</div>
      <div class="form-container">
        <a-form :wrapper-col="{span: 24}">
          <a-form-item has-feedback v-bind="validateInfos.username">
            <a-input v-model:value="modelRef.username" size="large">
              <template #prefix>
                <user-outlined></user-outlined>
              </template>
            </a-input>
          </a-form-item>
          <a-form-item has-feedback v-bind="validateInfos.password">
            <a-input-password v-model:value="modelRef.password" size="large">
              <template #prefix>
                <lock-outlined></lock-outlined>
              </template>
            </a-input-password>
          </a-form-item>
          <a-form-item>
            <a-button
                @click.prevent="handleLogin"
                type="primary"
                size="large"
                html-type="submit"
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
        <div class="login-tips">
          <div>
            <span>用户名：admin</span>
            <span>&nbsp;密码：admin</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { reactive, toRaw } from 'vue';
  import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
  import { useForm } from '@ant-design-vue/use';
  import { useStore } from 'vuex';

  export default {
    name: "LoginContainer",
    setup() {
      const modelRef = reactive({
        username: 'admin',
        password: 'admin'
      });
      const ruleRef = reactive({
        username: [{ required: true, message: '请输入用户名' }],
        password: [{ required: true, message: '请输入密码' }]
      });
      const { validate, validateInfos } = useForm(modelRef, ruleRef);
      const vStore = useStore();
      const handleLogin =() => {
        validate().then(() => {
          const values = toRaw(modelRef);
          vStore.dispatch('login', {data: values});
        }).catch((e) => {
          console.log('调试输出error', e)
        })
      };
      return {
        modelRef,
        handleLogin,
        validateInfos
      }
    },
    components: {
      UserOutlined,
      LockOutlined
    }
  }
</script>

<style lang="less" scoped>
  .login-container{
    width: 100%;
    min-height: calc(100vh);
    background: #f0f2f5 url("../../../assets/background.svg") no-repeat 50%;
    background-size: 100%;
    position: relative;
    .content{
      min-width: 260px;
      width: 368px;
      margin: 0 auto;
      padding-top: 120px;
      .title{
        text-align: center;
        font-size: 33px;
        color: rgba(0, 0, 0, .85);
        font-weight: 600;
        margin-bottom: 24px;
      }
      .ant-btn-primary{
        width: 100%;
      }
      .login-tips{
        text-align: center;
        span:first-child{
          margin-right: 12px;
        }
      }
    }
  }
</style>